﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class ChiTietHoaDonBL
    {
        private DataAccess dataAccess;

        public ChiTietHoaDonBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách chi tiet hoa don
        /// </summary>
        /// <returns></returns>
   //     public DataTable GetData()
    //    {
            // cau query select
     //       string sql = "SELECT MaHD, MaHang, SoLuong, DonGia FROM tbl_ChiTietHoaDon ORDER BY MaHD DESC ";

     //       DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
     //       dt = dataAccess.GetData(sql);
     //       return dt;
     //   }

        /// <summary>
        /// 2. Thêm chi tiet hoa don
        /// </summary>
        /// <param name="obj">ChiTietHoaDon</param>
        /// <returns>true</returns>
        public bool InsertData(ChiTietHoaDon obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };
            param[1] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };
            param[2] = new SqlParameter("@SoLuong", SqlDbType.Int) { Value = obj.SoLuong };
            param[3] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };
            
            // cau query select
            string sql = "INSERT INTO tbl_ChiTietHoaDon (MaHD, MaHang, SoLuong, DonGia) VALUES (@MaHD, @MaHang, @SoLuong,@DonGia)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa chi tiet hoa don
        /// </summary>
        /// <param name="MaHD, MaHang">MaHD,MaHang INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaHD, int MaHang)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };
            param[1] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = MaHang };

            // cau query select
            string sql = "DELETE FROM tbl_ChiTietHoaDon WHERE (MaHD = @MaHD) and (MaHang = @MaHang)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa chi tiet hoa don
        /// </summary>
        /// <param name="obj">ChiTietHoaDon</param>
        /// <returns>true</returns>
        public bool UpdateData(ChiTietHoaDon obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@SoLuong", SqlDbType.Int) { Value = obj.SoLuong };
            param[1] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };
            param[2] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };
            param[3] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            // cau query select
            string sql = "UPDATE tbl_ChiTietHoaDon SET SoLuong = @SoLuong, DonGia = @DonGia " +
                           " WHERE (MaHD = @MaHD) and (MaHang = @MaHang)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Hien thi cac loai hang cua hoa don
        /// </summary>
        /// <param name="MaHD"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };
            // cau query select
            string sql = "SELECT * FROM tbl_ChiTietHoaDon WHERE MaHD = @MaHD";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
}
